package com.xgst.bmg.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @BelongsProject: screen-win
 * @BelongsPackage: com.xgst.bmg.utils
 * @Author: xgstjs-11
 * @CreateTime: 2021-06-09 15:24:35
 * @Description: 图片处理工具类
 */
public class GraphicsUtil {
/**
      *
      * @Title: 构造图片
      * @Description: 生成水印并返回java.awt.image.BufferedImage
      * @param file
      *            源文件(图片)
      * @param waterFile
      *            水印文件(图片)
      * @param alpha
      *            透明度, 选择值从0.0~1.0: 完全透明~完全不透明
      * @return BufferedImage
      * @throws IOException
      */
   public static BufferedImage watermark(File file, File waterFile, float alpha) throws IOException {
           // 获取底图
           BufferedImage buffImg = ImageIO.read(file);
           // 获取层图
           BufferedImage waterImg = ImageIO.read(waterFile);
           //调用缩放方法获取缩放后的图片
           Image img = waterImg.getScaledInstance(228 , 90, Image.SCALE_SMOOTH);
           //创建一个新的缓存图片
           BufferedImage waterImg1 = new BufferedImage(228, 90, BufferedImage.TYPE_INT_RGB);
           //获取画笔
           Graphics2D graphics = waterImg1.createGraphics();
           //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
           graphics.drawImage(img, 0, 0,null);
           //一定要释放资源
           graphics.dispose();
           // 创建Graphics2D对象，用在底图对象上绘图
           Graphics2D g2d = buffImg.createGraphics();
           int waterImgWidth = waterImg1.getWidth();// 获取层图的宽度
           int waterImgHeight = waterImg1.getHeight();// 获取层图的高度
           // 在图形和图像中实现混合和透明效果
           g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
           // 绘制
           g2d.drawImage(waterImg, buffImg.getWidth()/10*7, buffImg.getHeight()/10*9, waterImgWidth, waterImgHeight, null);
           g2d.dispose();// 释放图形上下文使用的系统资源
           return buffImg;
       }

        /**
     * 输出水印图片
     *
     * @param buffImg
     *            图像加水印之后的BufferedImage对象
     * @param savePath
     *            图像加水印之后的保存路径
     */
     public void generateWaterFile(BufferedImage buffImg, String savePath) {
             int temp = savePath.lastIndexOf(".") + 1;
             try {
                     ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
                 } catch (IOException e1) {
                     e1.printStackTrace();
                 }
      }

    public void savePic(MultipartFile file, String picName) throws IOException {
        OutputStream os = null;
        InputStream inputStream = null;
        String fileName = null;
        String suffix = null;
        inputStream = file.getInputStream();
        fileName = file.getOriginalFilename();
        suffix = fileName.substring(fileName.lastIndexOf("."));
        String path = "C:\\file\\";
        byte[] bs = new byte[1024];
        int len;
        File tempFile = new File(path);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        os = new FileOutputStream(tempFile.getPath() + File.separator + picName + suffix);
        while ((len = inputStream.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        os.close();
        inputStream.close();
    }

}
